/*
 * To change this templine_para_ate, choose Tools | Templine_para_ates
 * and open the templine_para_ate in the editor.
 */
package com.android.firstgame;

/**
 *
 * @author danh.ngothanh
 */
public class Bullet {
    
    float bulletx = 0;
    float bullety = 0;
    float tagetx = 0;
    float tagety = 0;
    float line_para_a, line_para_b, line_para_c;
    int bullet_speed = 20;
    boolean movedirction = false;
    boolean xdirection = false;
    boolean ydirection = false;
    public Bullet(float bulletx, float bullety, float tagetx, float tagety){
        this.bulletx = bulletx;
        this.bullety = bullety;
        this.tagetx = tagetx;
        this.tagety = tagety;
        createlinefuntion(bulletx, bullety, tagetx, tagety);
    }
     final void createlinefuntion(float bulletx, float bullety, float tagetx, float tagety){
        line_para_a = (tagety - bullety);
        line_para_b = (-1)*(tagetx - bulletx);
        line_para_c = ((-1)*(tagety - bullety)*(bulletx) + (tagetx - bulletx)*bullety);
        if(Math.abs(tagetx - bulletx) > Math.abs(tagety - bullety)){
            movedirction = true;
            if(bulletx < tagetx){
                xdirection = true;
            }else{
                bulletx -= bullet_speed;
                bullety = recaline_para_culine_para_atey(bulletx);
            }
        }else{
            movedirction = false;
            if(bullety < tagety){
                ydirection = true;
            }else{
                bullety -= bullet_speed;
                bulletx = recaline_para_culine_para_atex(bullety);
            }
        }

    }

     public void setSpeed(int newspeedinpixel){
        bullet_speed = newspeedinpixel;
     }

     public boolean isHitTarget(float targetposx, float targetposy, int target_radius){
         if((bulletx > targetposx - target_radius) && (bulletx < targetposx + target_radius) && (bullety > targetposy - target_radius) && (bullety < targetposy + target_radius)){
             return true;
         }
        return false;
     }
    public void relocation(){
        if(movedirction){
            if(xdirection){
                bulletx += bullet_speed;
                bullety = recaline_para_culine_para_atey(bulletx);
            }else{
                bulletx -= bullet_speed;
                bullety = recaline_para_culine_para_atey(bulletx);
            }
        }else{
            if(ydirection){
                bullety += bullet_speed;
                bulletx = recaline_para_culine_para_atex(bullety);
            }else{
                bullety -= bullet_speed;
                bulletx = recaline_para_culine_para_atex(bullety);
            }
        }
    }
    public boolean checkvisible(int screen_width, int screen_height){
        if((bulletx<0)||(bulletx > screen_width)||(bullety<0)||(bullety > screen_height))
            return false;
        else
            return true;
    }
     public float recaline_para_culine_para_atey(float newx){
        float returny = 0;
        if(line_para_b != 0)
          returny = ((-1)*line_para_a*newx - line_para_c)/line_para_b;
        return returny;
    }
    public float recaline_para_culine_para_atex(float newy){
        float returnx = 0;
        if(line_para_a != 0)
                returnx = ((-1)*line_para_b*newy- line_para_c)/line_para_a;
        return returnx;
    }
    public void recaline_para_culine_para_ate(){
        if(Math.abs(tagetx - bulletx)>Math.abs(tagety - bullety)){
            if(tagetx > bulletx){
                bulletx ++;
            }
            else if(tagetx < bulletx)
                bulletx --;
            //The vao phuong trinh duong thang
            bullety = recaline_para_culine_para_atey(bulletx);
        }
        else{
            if(tagety > bullety)
                bullety ++;
            else if(tagety < bullety)
                bullety --;
            //The vao phuong trinh duong thang
            tagety = recaline_para_culine_para_atex(bullety);
        }
    }
    public void setBulletX(float new_x){
        bulletx = new_x;
    }
    public void setBulletY(float new_y){
        bullety = new_y;
    }
	public float getBulletX(){
        return bulletx;
    }
    public float getBulletY(){
        return bullety;
    }

    @Override
    public void finalize() throws Throwable{        
        bulletx = 0;
        bullety = 0;
        line_para_a = 0;
        line_para_b = 0;
        line_para_c = 0;
        super.finalize();
    }
    
}
